package com.zhlb.zfba.modules.per.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.mchange.v2.c3p0.stmt.GooGooStatementCache;
import com.zhlb.zfba.common.config.Global;
import com.zhlb.zfba.common.persistance.Msg;
import com.zhlb.zfba.common.utils.DateUtil;
import com.zhlb.zfba.modules.per.bean.PoliceInfo;
import com.zhlb.zfba.modules.per.bean.PolicePathLog;
import com.zhlb.zfba.modules.per.bean.RoomInfo;
import com.zhlb.zfba.modules.per.bean.SuspectInfo;
import com.zhlb.zfba.modules.per.bean.SuspectPathLog;
import com.zhlb.zfba.modules.per.service.PoliceInfoService;
import com.zhlb.zfba.modules.per.service.RoomInfoService;
import com.zhlb.zfba.modules.per.service.SuspectInfoService;
import com.zhlb.zfba.modules.per.service.SuspectPathLogService;

@Controller
@RequestMapping(value = "/per/suspectInfo")
public class SuspectInfoController {
	@Autowired
	private SuspectInfoService suspectInfoService;
	@Autowired
	RoomInfoService roomInfoService;
	@Autowired
	SuspectPathLogService suspectPathLogService;
	@RequestMapping(value = {"getSuspectInfo"})
	@ResponseBody
	public SuspectInfo getPolice(SuspectInfo suspectInfo) {
		SuspectInfo suspectInfoEntity =suspectInfoService.get(suspectInfo.getInfoGUID());
		return suspectInfoEntity;
	}
	@RequestMapping(value = {"list", ""})
	public String list(SuspectInfo suspectInfo,String isHistoryFlg,String isFirst, HttpServletRequest request, HttpServletResponse response, Model model) {
		//第一访问，默认显示当天的
		if("1".equals(isFirst)){
		String currentData=DateUtil.dateToStr(new Date(), "yyyy-MM-dd");
		suspectInfo.setInTime(currentData);
		}
		// 引入PageHepler插件
		String pageSizeStr=Global.getConfig("pagesize");
		if (!StringUtils.isNumeric(pageSizeStr)){
			pageSizeStr="10";
		}
		Integer pageSize=Integer.parseInt(pageSizeStr);
		Integer pn=0;
		if (StringUtils.isNumeric(request.getParameter("pageNo"))){
			pn=Integer.parseInt(request.getParameter("pageNo"));
		}
		//
		//获取询问室下拉框的值
			RoomInfo roomInfo =new RoomInfo();
			roomInfo.setRoomType(4);
			List<RoomInfo> inquestRoomInfoList=roomInfoService.findList(roomInfo);
		//允许区域
//			RoomInfo roomInfo2 =new RoomInfo();
//			roomInfo.setOtherType(4);
			List<RoomInfo> permitRoomInfoList=roomInfoService.findList(new RoomInfo());
		//
		PageHelper.startPage(pn, pageSize);
		PageInfo pageInfo = null;
		//在所信息时，根据同案标识分组
		List<SuspectInfo> list = suspectInfoService.findList(suspectInfo);
		if ("1".equals(isHistoryFlg)) {
			pageInfo = new PageInfo(list, 5);
		} else {
			List<SuspectInfo> accompliceSignList = suspectInfoService.findAccompliceSigns(new SuspectInfo());
			List<SuspectInfo> currentlist =this.gorupByAccompliceSign(accompliceSignList, list);
			pageInfo = new PageInfo(currentlist, 5);

		}
		model.addAttribute("suspectInfo", suspectInfo);//查询条件回显
		model.addAttribute("inquestRoomInfoList", inquestRoomInfoList);//询问室下拉框
		model.addAttribute("permitRoomInfoList", permitRoomInfoList);//允许区域
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("pageInfoJson", JSON.toJSONString(pageInfo));
		if("1".equals(isHistoryFlg)){
			return "modules/per/suspectInfoHistoryList";
		}else{
		return "modules/per/suspectInfoList";
		}
	}
	@RequestMapping(value = {"listNoPage"})
	public String listNoPage(SuspectInfo suspectInfo, Model model) {
		//第一访问，默认显示当天的
//		if("1".equals(isFirst)){
//		String currentData=DateUtil.dateToStr(new Date(), "yyyy-MM-dd");
//		suspectInfo.setInTime(currentData);
//		}
		//获取所有的被询问人信息（AccompliceSign，"InTime"）
		List<SuspectInfo> accompliceSignList =suspectInfoService.findAccompliceSigns(new SuspectInfo());
		List<SuspectInfo> suspectInfoList=suspectInfoService.findList(suspectInfo);
		model.addAttribute("suspectInfo", suspectInfo);//查询条件回显
		model.addAttribute("suspectInfoList", this.gorupByAccompliceSign(accompliceSignList,suspectInfoList));
			return "modules/per/suspectInfoListNoPage";
	}

	public List<SuspectInfo> gorupByAccompliceSign(List<SuspectInfo> accompliceSignList,
			List<SuspectInfo> suspectInfoList) {
		List<SuspectInfo> resultList = Lists.newArrayList();
		// 1.捡出同案标记AccompliceSign为空的List
				for (SuspectInfo temp3 : suspectInfoList) {
					if (StringUtils.isBlank(temp3.getAccompliceSign())) {
						resultList.add(temp3);
					}
				}
		// 2.按标记分组
		int num = 0;
		for (SuspectInfo temp1 : accompliceSignList) {
			if(temp1==null||StringUtils.isBlank(temp1.getAccompliceSign())){//修复空指针bug
				continue;
			}
			int addNum=0;
			for (SuspectInfo temp2 : suspectInfoList) {

				if (temp1.getAccompliceSign().equals(temp2.getAccompliceSign())) {
					addNum++;
					if (num % 2 == 0) {
						temp2.setTrbkclass("oddColor");
					} else {
						temp2.setTrbkclass("evenColor");
					}
					resultList.add(temp2);
				}
			}
			if(addNum>0){//当前同案标记下有数据时，页面才显示这个分组，用于翻页的情况下
				num = num + 1;
			}
		}
		
		suspectInfoList.clear();
		suspectInfoList.addAll(resultList);
		return suspectInfoList;
	}
	@RequestMapping(value = "form")
	public String form(PoliceInfo policeInfo, Model model) {
		model.addAttribute("policeInfo", policeInfo);
		return "modules/task/policeInfo";
	}

/*	@RequestMapping(value = "save")
	public String save(PoliceInfo policeInfo, Model model, RedirectAttributes redirectAttributes) {
//		if (!beanValidator(model, policeInfo)){
//			return form(taskJobConf, model);
//		}
		policeInfoService.save(policeInfo);
		//addMessage(redirectAttributes, "保存任务计划配置成功");
		//return "redirect:"+Global.getAdminPath()+"/task/taskJobConf/?repage";
		return "";
	}*/
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Msg saveSuspectInfo(SuspectInfo suspectInfo, BindingResult result) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		if (result.hasErrors()) {
//			List<FieldError> errors = result.getFieldErrors();
//			for (FieldError error : errors) {
//				map.put(error.getField(), error.getDefaultMessage());
//
//			}
//			return Msg.failure().add("errorFileds", map);
//		}
		suspectInfoService.save(suspectInfo);
		return Msg.success();
	}
	
	@RequestMapping(value = "delete")
	@ResponseBody
	public Msg delete(String ids) {
		suspectInfoService.delete(ids);
		return Msg.success();
	}
	@RequestMapping(value = "isLeave")
	@ResponseBody
	public Msg isLeave(SuspectInfo suspectInfo) {
		suspectInfoService.updateIsLeave(suspectInfo);
		return Msg.success();
	}
	@RequestMapping(value = "checkSuspectPathView")
	public String checkPathListofAndChart(SuspectPathLog suspectPathLog, Model model) {
		List<SuspectPathLog> suspectPathLogList= suspectPathLogService.findList(suspectPathLog);
		List<SuspectPathLog> suspectPathLogList2= suspectPathLogService.findList(suspectPathLog);
		model.addAttribute("suspectPathLogList", suspectPathLogList);
		List<SuspectPathLog> suspectPathLogChartList=new ArrayList<SuspectPathLog>();
		for(SuspectPathLog temp:suspectPathLogList2){
			if(StringUtils.isNoneBlank(temp.getInTime())||temp.getInTime().length()>=18){
				int endPos=19;//2017/10/26 8:50:33
				if(temp.getInTime().length()<19){//2017-10-26 08:57:39
					endPos=18;
				}
				String intimeStr=temp.getInTime().substring(11, endPos);
				temp.setInTime(intimeStr);
			}
			suspectPathLogChartList.add(temp);
		}
		model.addAttribute("canvasWidth",suspectPathLogChartList.size()*65);
		model.addAttribute("suspectPathLogListJson", JSON.toJSONString(suspectPathLogChartList));
		return "modules/per/checkSuspectPathView";
	}
	
	@RequestMapping(value = "accompliceSign")
	@ResponseBody
	public Msg accompliceSign(String ids) {
		suspectInfoService.accompliceSign(ids);
		return Msg.success();
	}
}
